package com.iman.Confucius.service.impl.student;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.Confucius.domain.QuizAnswers;
import com.iman.Confucius.domain.QuizStudent;
import com.iman.Confucius.domain.QuizTeacher;
import com.iman.Confucius.domain.User;
import com.iman.Confucius.service.RegisterService;
import com.iman.Confucius.service.student.ClazzService;
import com.iman.Confucius.service.student.QuestionService;
import com.iman.Confucius.vo.AnswerVo;
import com.iman.Confucius.vo.QuestionVo;
import com.iman.Confucius.vo.teacher.TeacherClassPair;
import com.iman.common.hibernate.HibernateSupport;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.support.Page;
import com.iman.common.util.IdCreater;

@Service("questionService")
public class QuestionServiceImpl implements QuestionService{
	
	@Inject
	TemplateQLService templateQLService;

	@Inject
	HibernateSupport hibernateSupport;

	@Inject
	RegisterService reisterService;

	@Inject
	ClazzService classService;

	@Override
	public Page<QuestionVo> getQuestionList(Map<String, Integer> pageParam,
			Long userId, boolean answered, Long courseId, Long teacherId,
			Long classId) {
		Integer currentPage = (Integer) pageParam.get("pageIndex");
		Integer pageSize = (Integer) pageParam.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("ownerId", userId);
		hashMapParams.put("answered", answered);
		if (courseId != null) {
			hashMapParams.put("courseId", courseId);
		}
		if (teacherId != null) {
			hashMapParams.put("teacherId", teacherId);
			hashMapParams.remove("ownerId");
			if (classId != null) {
				hashMapParams.put("classId", classId);
			}
		}
		Page<QuestionVo> list = templateQLService.findByPagedSQLBeanQuery(
				"query-questions-byOwner", hashMapParams,
				Page.getStartOfPage(currentPage, pageSize), pageSize,
				QuestionVo.class);
		return list;
	}

	@Override
	public Page<QuestionVo> getALLQuestionList(Map<String, Integer> pageParam,
			Long courseId, Long teacherId) {
		Integer currentPage = (Integer) pageParam.get("pageIndex");
		Integer pageSize = (Integer) pageParam.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		if (courseId != null) {
			hashMapParams.put("courseId", courseId);
		}
		if (teacherId != null) {
			hashMapParams.put("teacherId", teacherId);
		}
		Page<QuestionVo> list = templateQLService.findByPagedSQLBeanQuery(
				"query-questions-byCourse", hashMapParams,
				Page.getStartOfPage(currentPage, pageSize), pageSize,
				QuestionVo.class);
		return list;
	}

	@Override
	public Page<QuestionVo> getHotQuestionList(int questionCount,
			Long courseId, Long teacherId) {
		Integer currentPage = 1;
		Integer pageSize = questionCount;
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		if (courseId != null) {
			hashMapParams.put("courseId", courseId);
		}
		if (teacherId != null) {
			hashMapParams.put("teacherId", teacherId);
		}
		Page<QuestionVo> list = templateQLService.findByPagedSQLBeanQuery(
				"query-hot-questions", hashMapParams,
				Page.getStartOfPage(currentPage, pageSize), pageSize,
				QuestionVo.class);
		return list;
	}

	@Override
	public Long postQuestion(Long courseId, String title, String content,
			long userId) {
		User user = reisterService.getUser(userId);
		if (user == null) {
			throw new IllegalArgumentException("无效用户标识");
		}
		List<TeacherClassPair> teacherIdList = classService
				.getTeacherClassPairByStudent(userId);

		Date currentDate = new Date();
		QuizStudent quizStudent = new QuizStudent();
		// id,courseId,title,content,create_dt,quiz_uid,view_cnt,del_yn,update_dt,answer_cnt
		quizStudent.setQuizId(IdCreater.getId());
		quizStudent.setCourseId(courseId);
		quizStudent.setTitle(title);
		quizStudent.setContent(content);
		quizStudent.setQuizUid(userId);
		quizStudent.setCreateDate(currentDate);
		quizStudent.setViewCnt(0);
		quizStudent.setDelYn(0);
		quizStudent.setUpdateDate(currentDate);
		quizStudent.setAnswerCnt(0);
		Long result = (Long) hibernateSupport.save(quizStudent);
		List<QuizTeacher> quizTeacherList = new ArrayList<QuizTeacher>();
		if (teacherIdList != null) {
			for (TeacherClassPair tcPair : teacherIdList) {
				QuizTeacher quizTeacher = new QuizTeacher();
				quizTeacher.setUniqueId(IdCreater.getId());
				quizTeacher.setTeacherId(tcPair.getTeacherId());
				quizTeacher.setQuizId(result);
				quizTeacher.setQuizDate(currentDate);
				quizTeacher.setCourseId(courseId);
				quizTeacher.setClassId(tcPair.getClassId());
				quizTeacher.setAnsered(false);
				quizTeacherList.add(quizTeacher);
			}
		}
		hibernateSupport.saveAll(quizTeacherList);
		return result;
	}

	@Override
	public QuestionVo getQuestionDetail(Long questionId) {
		if(questionId == null){
			throw new IllegalArgumentException("鏃犳晥闂鏍囪瘑");
		}
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("questionId", questionId);
		QuestionVo question = templateQLService.findObjectByQuery("query-question-detail", hashMapParams, QuestionVo.class);
		if(question!=null ){
			List<AnswerVo> answerList = templateQLService.findBySQLBeanQuery("query-anwers", hashMapParams, AnswerVo.class);
			question.setAnswersOfQuestion(answerList);
		}
		return question;
	}
	
	@Override
	public void updateQuestionViewcnt(Long questionId){
		if(questionId == null){
			throw new IllegalArgumentException("鏃犳晥闂鏍囪瘑");
		}
		QuizStudent quizStudent = hibernateSupport.get(QuizStudent.class, questionId);
		if(quizStudent!=null){
			quizStudent.setViewCnt(quizStudent.getViewCnt() + 1);
			hibernateSupport.update(quizStudent);
		}
	}
	
	@Override
	public void replayQuestion(Long questionId, Long userId, String content, int answerTp){
		if(questionId == null){
			throw new IllegalArgumentException("鏃犳晥闂鏍囪瘑");
		}
		QuizStudent quizStudent = hibernateSupport.get(QuizStudent.class, questionId);
		if(quizStudent == null){
			throw new IllegalArgumentException("涓嶅瓨鍦ㄧ殑'闂'鏍囪瘑 " + questionId);
		}
		if(userId == null ){
			throw new IllegalArgumentException("鏃犳晥鐢ㄦ埛鏍囪瘑");
		}
		User user = hibernateSupport.get(User.class, userId);
		if(user == null){
			throw new IllegalArgumentException("涓嶅瓨鍦ㄧ殑'鐢ㄦ埛'鏍囪瘑 " + questionId);
		}
		QuizAnswers answer = new QuizAnswers();
		answer.setAnswerId(IdCreater.getId());
		answer.setAnswerTp(answerTp);
		answer.setContent(content);
		answer.setDelYn(0);
		answer.setQuizId(questionId);
		answer.setReplayDate(new Date());
		answer.setUserId(userId);
		hibernateSupport.save(answer);
		quizStudent.setAnswerCnt(quizStudent.getAnswerCnt() + 1);
	}

}
